package br.com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import org.apache.log4j.Logger;

import br.com.logistica.util.dto.ServiceDTO;
import br.com.logistica.vo.NotaFiscal;
import br.com.smart.logistica.services.NotaFiscalSCN;

public class ThreadLeituraArquivo extends Thread {
	private static final String FOLDER_PATH = "C:\\Users\\MAQUINA2\\Logistica\\Logistica\\WebContent\\resources\\";
	private static Logger logger = Logger.getLogger(ThreadLeituraArquivo.class);

	@Override
	public void run() {

		// //implemetar aqui a regra de negocio para que ocorra o push
		// da nota fiscal

		// moverArquivo( "NotaFiscalInicio", "NotaFiscalEnviada", string
		// ) ;

		Leitura leitura = new Leitura();
		leitura.parser("C:\\Users\\MAQUINA2\\Desktop\\Projetos\\Log5\\teste.xml");

		NotaFiscal fiscal = leitura.getNota();

		ServiceDTO dto = new ServiceDTO();
		dto.set("notaFiscal", fiscal);
		try {
			NotaFiscalSCN.insereCliente(dto);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// /try
		// ///Logica robo
		// ///valida a nota
		// /persisti na base de dados
		// //consome webservice transportadora
		// ///////
		// /////catch nota errada!!!!!
		// ///mover para o diretorio de notas erradas!!!!

	}

	private static void moverArquivo(String ori, String dest, String nomeArquivo)
			throws IOException {
		FileChannel oriChannel = new FileInputStream(new File(FOLDER_PATH + ori
				+ "\\" + nomeArquivo)).getChannel();
		FileChannel destChannel = new FileOutputStream(FOLDER_PATH + dest
				+ "\\" + nomeArquivo).getChannel();

		destChannel.transferFrom(oriChannel, 0, oriChannel.size());

		oriChannel.close();
		destChannel.close();

		File exit = new File(FOLDER_PATH + ori + "\\" + nomeArquivo);

		exit.delete();
	}
}
